package com.hcb.app911;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.hcb.common.ActionEvent;
import com.hcb.constants.Constants;
import com.hcb.controller.CustomerController;
import com.hcb.dto.CustomerDTO;
import com.hcb.model.utils.CustomerModel;
import com.hcb.webservice.ServiceHandler;
import com.hcb.webservice.WebService;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ValidationActivity extends Activity implements OnClickListener {
	Button btnverify;
	EditText edAccessCode;
	int idaler;
	Dialog dialog;
	AnimationDrawable frameAnimation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_number_validation);

		Bundle data = getIntent().getExtras();
		if (data != null) {
			idaler = data.getInt("arler");
		}

		btnverify = (Button) findViewById(R.id.btnverify);
		btnverify.setOnClickListener(this);
		edAccessCode = (EditText) findViewById(R.id.edAccessCode);

	}

	public class VerySlowTask extends AsyncTask<Void, Void, Void> {
		String result;
		CustomerDTO customer;

		public VerySlowTask(CustomerDTO cus) {
			customer = cus;
			dialog = new Dialog(ValidationActivity.this, R.style.NewDialog);
			dialog.setContentView(R.layout.loading);
			
			dialog.setCancelable(false);
			dialog.findViewById(R.id.loading_icon).setBackgroundResource(
					R.drawable.spin_animation);
			frameAnimation = (AnimationDrawable) dialog.findViewById(
					R.id.loading_icon).getBackground();
			dialog.show();
			frameAnimation.start();
		}

		@Override
		protected Void doInBackground(Void... params) {

			ServiceHandler sh = new ServiceHandler();
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("query",
					Constants.UPDATE_CUSTOMER_STATUS));
			nameValuePairs.add(new BasicNameValuePair("api_secret_key",
					Constants.API_KEY));
			nameValuePairs.add(new BasicNameValuePair(Constants.CUSTOMER_ID,
					customer.getCustomer_id()));
			nameValuePairs.add(new BasicNameValuePair(Constants.ACCESS_CODE,
					edAccessCode.getText().toString().trim()));

			String jsonStr = sh.makeServiceCall(Constants.URL_SERVICE,
					ServiceHandler.POST, nameValuePairs);
			if (jsonStr != null) {
				try {
					JSONObject object;
					object = new JSONObject(jsonStr);
					String value = object.getString("result");
					if (value.contains("success")) {

						ActionEvent e = new ActionEvent(
								ValidationActivity.this,
								Constants.UPDATE_CUSTOMER, customer, null);
						CustomerController.getInstance().handleViewEvent(e);

						
						
						Intent intent = new Intent(ValidationActivity.this,
								MainActivity.class);
						// Bundle bundle1 = new Bundle();
						// bundle1.putSerializable("customer", customer);
						// intent.putExtras(bundle1);
						startActivity(intent);
						finish();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else {
				// Log.e("ServiceHandler",
				// "Couldn't get any data from the url");
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
		}

	}

	public class VerySlowTaskEXITS extends AsyncTask<Void, Void, Void> {
		String result;
		CustomerDTO customer;

		public VerySlowTaskEXITS(CustomerDTO cus) {
			customer = cus;
			dialog = new Dialog(ValidationActivity.this, R.style.NewDialog);
			dialog.setContentView(R.layout.loading);
			
			dialog.setCancelable(false);
			dialog.findViewById(R.id.loading_icon).setBackgroundResource(
					R.drawable.spin_animation);
			frameAnimation = (AnimationDrawable) dialog.findViewById(
					R.id.loading_icon).getBackground();
			dialog.show();
			frameAnimation.start();
		}

		@Override
		protected Void doInBackground(Void... params) {

			ServiceHandler sh = new ServiceHandler();
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("query",
					Constants.GET_CUSTOMER_ID_VERICODE));
			nameValuePairs.add(new BasicNameValuePair("api_secret_key",
					Constants.API_KEY));
			nameValuePairs.add(new BasicNameValuePair(Constants.ACCESS_CODE,
					edAccessCode.getText().toString().trim()));

			String jsonStr = sh.makeServiceCall(Constants.URL_SERVICE,
					ServiceHandler.POST, nameValuePairs);
			if (jsonStr != null) {
				try {
					JSONObject object;
					object = new JSONObject(jsonStr);
					String value = object.getString("result");
					String idget = object.getString("data_body");
					if (value.contains("success")) {
						Intent intent = new Intent(ValidationActivity.this,
								MainActivity.class);
						Bundle bundle = new Bundle();
						bundle.putString("id", idget);
						intent.putExtras(bundle);
						// Bundle bundle1 = new Bundle();
						// bundle1.putSerializable("customer", customer);
						// intent.putExtras(bundle1);
						startActivity(intent);
						finish();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else {
				// Log.e("ServiceHandler",
				// "Couldn't get any data from the url");
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();

		}
	}

	private class validation implements Runnable {

		CustomerDTO customer;
		Context contextRunnable;

		public validation(CustomerDTO cus, Context context) {
			customer = cus;
			contextRunnable = context;
		}

		@Override
		public void run() {
			String url = Constants.URL_SERVICE + "updateCustomerStatus";
			url += "/api_secret_key/" + "3832_2hd3hdiwyu34eyeuidhd_948";
			url += "/query/" + "updateCustomerStatus";
			url += "/customer_id/" + customer.getCustomer_id();
			url += "/access_code/" + edAccessCode.getText().toString().trim();
			String jsonResponse = WebService.getInstance().get(url);
			try {
				JSONObject object;
				object = new JSONObject(jsonResponse);
				String value = object.getString("result");
				if (value.contains("success")) {

					ActionEvent e = new ActionEvent(ValidationActivity.this,
							Constants.UPDATE_CUSTOMER, customer, null);
					CustomerController.getInstance().handleViewEvent(e);

					Intent intent = new Intent(ValidationActivity.this,
							MainActivity.class);
					// Bundle bundle1 = new Bundle();
					// bundle1.putSerializable("customer", customer);
					// intent.putExtras(bundle1);
					startActivity(intent);
					finish();
				}
			} catch (JSONException e) {
				e.printStackTrace();
				return;
			}

		}

	}

	private class validationAler implements Runnable {

		CustomerDTO customer;
		Context contextRunnable;

		public validationAler(CustomerDTO cus, Context context) {
			customer = cus;
			contextRunnable = context;
		}

		@Override
		public void run() {
			String url = Constants.URL_SERVICE
					+ "getCustomerIdWithVerificationCode";
			url += "/api_secret_key/" + "3832_2hd3hdiwyu34eyeuidhd_948";
			url += "/query/" + "getCustomerIdWithVerificationCode";
			url += "/access_code/" + edAccessCode.getText().toString().trim();
			String jsonResponse = WebService.getInstance().get(url);
			try {
				JSONObject object;
				object = new JSONObject(jsonResponse);
				String value = object.getString("result");
				String idget = object.getString("data_body");
				if (value.contains("success")) {
					String urlget = Constants.URL_SERVICE
							+ "getCustomerIdWithVerificationCode";
					urlget += "/api_secret_key/"
							+ "3832_2hd3hdiwyu34eyeuidhd_948";
					urlget += "/query/" + "getCustomerDetails";
					urlget += "/customer_id/"
							+ edAccessCode.getText().toString().trim();
					String jsonget = WebService.getInstance().get(urlget);

					// ActionEvent e = new ActionEvent(ValidationActivity.this,
					// Constants.UPDATE_CUSTOMER, customer, null);
					// CustomerController.getInstance().handleViewEvent(e);

					Intent intent = new Intent(ValidationActivity.this,
							MainActivity.class);

					Bundle bundle = new Bundle();
					bundle.putString("id", idget);
					intent.putExtras(bundle);
					// Bundle bundle1 = new Bundle();
					// bundle1.putSerializable("customer", customer);
					// intent.putExtras(bundle1);
					startActivity(intent);
					finish();
				}
			} catch (JSONException e) {
				e.printStackTrace();
				return;
			}

		}

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btnverify:
			CustomerDTO cus = new CustomerDTO();

			if (idaler == 2) {
				new VerySlowTaskEXITS(cus).execute();
//				validationAler aler = new validationAler(cus, this);
//				new Thread(aler).start();
			} else {
				CustomerModel model = CustomerModel.getInstance();
				cus = model.getCustomer(this);
				new VerySlowTask(cus).execute();
				// validation regis = new validation(cus, this);
				// new Thread(regis).start();
			}

			break;

		default:
			break;
		}

	}
}
